/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.momarketing.service;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;
import com.boxin.ims.modules.momarketing.entity.PageVisit;
import com.boxin.ims.modules.momarketing.dao.PageVisitDao;

/**
 * 页面访问Service
 * @author Jakemanse
 * @version 2014-05-22
 */
@Component
@Transactional(readOnly = true)
public class PageVisitService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(PageVisitService.class);
	
	@Autowired
	private PageVisitDao pageVisitDao;
	
	public PageVisit get(Long id) {
		return pageVisitDao.findOne(id);
	}
	
	public Page<PageVisit> find(Page<PageVisit> page, PageVisit pageVisit) {
		DetachedCriteria dc = pageVisitDao.createDetachedCriteria();
		if (pageVisit.getUser()!=null && pageVisit.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", pageVisit.getUser().getId()));
		}
		if (StringUtils.isNotEmpty(pageVisit.getName())){
			dc.add(Restrictions.like("name", "%"+pageVisit.getName()+"%"));
		}
		if (StringUtils.isNotEmpty(pageVisit.getType())){
			dc.add(Restrictions.eq("type", pageVisit.getType()));
		}
		if (StringUtils.isNotEmpty(pageVisit.getMobile())){
			dc.add(Restrictions.eq("mobile", pageVisit.getMobile()));
		}
		if(pageVisit.getCreateTime() !=null){
			String timeField = "createTime";
			dc.add(Restrictions.ge(timeField,pageVisit.getCreateTime() ));
		}
		if(pageVisit.getEndTime() !=null){
			String timeField = "createTime";
			dc.add(Restrictions.le(timeField,pageVisit.getEndTime()));
		}
		dc.addOrder(Order.desc("id"));
		return pageVisitDao.find(page, dc);
	}
	
	
	
	public void save(HttpServletRequest request,String type){
		PageVisit pageVisit = new PageVisit();
		pageVisit.setDetail("");
//		pageVisit.setName(name);
//		pageVisit.setPageId(pageId);
		
	}
	
	@Transactional(readOnly = false)
	public void save(PageVisit pageVisit) {
		pageVisitDao.save(pageVisit);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		pageVisitDao.deleteById(id);
	}
	
}
